package com.sumainfor.wx.tenant.controller;


import com.sumainfor.common.config.RedisUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.SendMessageUtils;
import com.sumainfor.wx.tenant.service.WechatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Map;

/**
 * 短信发送验证码然后进行验证
 *
 * @author xiaoma
 */
@RequestMapping("/SendMessageColler")
@Controller
public class SendMessageColler implements Serializable {
    private static final long serialVersionUID = 1L;

    Logger log = LoggerFactory.getLogger(SendMessageColler.class);

    @Autowired
    WechatService wxbiz;

    /**
     * redis
     */
    @Autowired
    private RedisUtils redis;

    /**
     * 用户注册调用阿里大于发送手机验证码
     *
     * @param params
     * @return result
     * @author xiaoma
     */
    @GetMapping("/SendAuthCode")
    @ResponseBody
    public JsonResult SendAuthCode(@RequestParam Map<String, Object> params, HttpSession session) throws Exception {
        JsonResult result = new JsonResult();
        log.info("=======发验证码："+params);
        if (params.get("phone") != null) {

            if (wxbiz.getCountPhone(params) > 0) {//用于后面的查询
                result.putFailed("该手机号码已绑定用户");
            } else {
                int number = (int) ((Math.random() * 9 + 1) * 100000);//随机生成6位数的随机数
                params.put("code", number);
                Map<String, Object> sendmap = SendMessageUtils.sendMssage(params);//调用阿里接口发送验证码
                if (sendmap.get("errorCode").toString().equals("200")) {
                    //存入redis里面
                    redis.set(params.get("phone").toString(), params.get("code").toString(), 120);//设置120秒过期
                    result.put(sendmap);
                } else {
                    log.info("sendmap---------"+sendmap);
//                    result.putFailed("服务器繁忙...");
                    result.put(sendmap);
                }
            }
        } else {
            result.putFailed("请输入手机号码");
        }

        return result;

    }


    /**
     * 用户更换手机号码调用阿里大于发送手机验证码
     *
     * @param params
     * @return result
     * @author xiaoma
     */
    @GetMapping("/changeMssage")
    @ResponseBody
    public JsonResult changeMssage(@RequestParam Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        if (params.get("phone") != null) {
            if(wxbiz.getCountPhone1(params)>0){

                params.remove("TenId");
                int number = (int) ((Math.random() * 9 + 1) * 100000);//随机生成6位数的随机数
                params.put("code", number);
                Map<String, Object> sendmap = SendMessageUtils.changeMssage(params);//调用阿里接口发送验证码
                if (sendmap.get("errorCode").toString().equals("200")) {
                    //存入redis里面
                    redis.set(params.get("phone").toString(), params.get("code").toString(), 120);
                    result.put(sendmap);
                } else {
                    result.putFailed("服务器繁忙...");
                }

            }else{
                result.putFailed("请输入旧手机号码");
            }

        } else {
            result.putFailed("请输入手机号码");
        }
        return result;
    }

    /**
     * 用户设置服务密码时发送验证码
     *
     * @param params
     * @return result
     * @author xiaoma
     */
    @GetMapping("/saveTenSerpadMssage")
    @ResponseBody
    public JsonResult saveTenSerpadMssage(@RequestParam Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        if (params.get("phone") != null) {
//            if(wxbiz.getCountPhone(params)>0){
//                result.putFailed("该手机号码已绑定用户");
//            }else{
            int number = (int) ((Math.random() * 9 + 1) * 100000);//随机生成6位数的随机数
            params.put("code", number);
            Map<String, Object> sendmap = SendMessageUtils.saveTenSerpadMssage(params);//调用阿里接口发送验证码
            if (sendmap.get("errorCode").toString().equals("200")) {
                //存入redis里面
                redis.set(params.get("phone").toString(), params.get("code").toString(), 120);
                result.put(sendmap);
            } else {
                result.putFailed("服务器繁忙...");
            }
            // }

        } else {
            result.putFailed("请输入手机号码");
        }
        return result;
    }



    /**
     * 用户注册验证6位数验证码是否正确
     *
     * @param
     * @return
     * @throws Exception
     */
    @GetMapping("/getcodeInfo")
    @ResponseBody
    public JsonResult getcodeInfo(@RequestParam Map<String, Object> parmas) throws Exception {
        JsonResult result = new JsonResult();
        if (redis.get(parmas.get("phone").toString()) != null) {
            if (parmas.get("code").toString().equals(redis.get(parmas.get("phone").toString()))) {
                result.put(parmas.get("phone").toString());
//                redis.delete(parmas.get("phone").toString());//验证成功了之后删除redis中的key
            } else {
                result.putFailed("验证码错误");
            }
        } else {
            result.putFailed("验证码过期,请重新验证");
        }
        return result;
    }


    /**
     * 用户更换手机号码验证6位数验证码是否正确
     *
     * @param parmas
     * @return
     * @throws Exception
     */
    @GetMapping("/getchangecodeInfo")
    @ResponseBody
    public JsonResult getchangecodeInfo(@RequestParam Map<String, Object> parmas) throws Exception {
        JsonResult result = new JsonResult();
        if (redis.get(parmas.get("phone").toString()) != null) {
            if (parmas.get("code").toString().equals(redis.get(parmas.get("phone").toString()))) {
                result.putSuccess("验证成功");
                redis.delete(parmas.get("phone").toString());//验证成功了之后删除redis中的key
            } else {
                result.putFailed("验证码错误");
            }
        } else {
            result.putFailed("验证码过期,请重新验证");
        }
        return result;
    }


    /**
     * 用户设置服务密码的时候验证验证码
     *
     * @param parmas
     * @return
     * @throws Exception
     */
    @GetMapping("/getTenSerpadMssage")
    @ResponseBody
    public JsonResult getTenSerpadMssage(@RequestParam Map<String, Object> parmas) throws Exception {
        JsonResult result = new JsonResult();
        if (redis.get(parmas.get("phone").toString()) != null) {
            if (parmas.get("code").toString().equals(redis.get(parmas.get("phone").toString()))) {
                result.putSuccess("验证成功");
                redis.delete(parmas.get("phone").toString());//验证成功了之后删除redis中的key
            } else {
                result.putFailed("验证码错误");
            }
        } else {
            result.putFailed("验证码过期,请重新验证");
        }
        return result;
    }

}
